@*
* Copyright 2015 Yahoo Inc. Licensed under the Apache License, Version 2.0
* See accompanying LICENSE file.
*@
@import scalaz.{\/}
@(cluster: String, topic: String, errorOrForm: kafka.manager.ApiError \/ (Form[models.form.AddTopicPartitions], kafka.manager.model.ClusterContext)
)(implicit af: features.ApplicationFeatures, messages: play.api.i18n.Messages, menus: models.navigation.Menus, request:RequestHeader)

@import helper._
@import controllers.routes

@theMenu = {
    @views.html.navigation.clusterMenu(cluster,"Topic","Add Partitions",menus.clusterMenus(cluster)(
        errorOrForm.toOption.map(_._2.clusterFeatures).getOrElse(kafka.manager.features.ClusterFeatures.default)))
}
        
@renderForm(addTopicPartitionsForm: Form[models.form.AddTopicPartitions]) = {
    @b4.vertical.form(routes.Topic.handleAddPartitions(cluster, topic)) { implicit fc =>
        <table class="table">
            <thead>
            <tr><th>Add Partitions</th><th>Brokers</th></tr>
            </thead>
            <tbody>
                <tr>
                <td>
                @b4.text(addTopicPartitionsForm("topic"), '_label -> "Topic", 'placeholder -> "", 'autofocus -> true )
                @b4.text(addTopicPartitionsForm("partitions"), '_label -> "Partitions", 'placeholder -> "8")
                @b4.hidden(addTopicPartitionsForm("readVersion").name,addTopicPartitionsForm("readVersion").value.getOrElse(-1))
                </td>
                <td>
                    <button type="button" class="btn btn-secondary" onClick="checkBoxSelect('brokers',true);">
                        <b>Select All</b>
                    </button>
                    <button type="button" class="btn btn-secondary" onClick="checkBoxSelect('brokers',false);">
                        <b>Select None</b>
                    </button>
                    @helper.repeat(addTopicPartitionsForm("brokers"), min = 1) { brokerSelectForm =>
                        @b4.hidden(brokerSelectForm("id").name,brokerSelectForm("id").value.getOrElse(-1))
                        @b4.hidden(brokerSelectForm("host").name,brokerSelectForm("host").value.getOrElse(""))
                        @b4.checkbox(brokerSelectForm("selected"), '_text -> s"${brokerSelectForm("id").value.getOrElse(-1)} - ${brokerSelectForm("host").value.getOrElse("")}")
                    }
                </td>
            </tr>
            </tbody>
        </table>
        @b4.submit('class -> "submit-button btn btn-primary"){ Add Partitions }
        <a href="@routes.Topic.topic(cluster,topic)" class="cancel-button btn btn-secondary" role="button">Cancel</a>
    }
}

@main(
    "Add Partitions",
    menu = theMenu,
    breadcrumbs=views.html.navigation.breadCrumbs(models.navigation.BreadCrumbs.withNamedViewAndClusterAndTopic("Topic View",cluster,topic,"Add Partitions"))) {
    <div class="col-md-6 un-pad-me">
        <div class="card">
            <div class="card-header"><h3><button type="button" class="btn btn-link" onclick="goBack()"><span class="octicon octicon-arrow-left" aria-hidden="true"></span></button>Add Partitions</h3></div>
            <div class="card-body">
                @errorOrForm.fold( views.html.errors.onApiError(_), t => renderForm(t._1))
            </div>
        </div>
    </div>
}

